/*
 * Copyright (C) 2014 Tolstykh Vladislav
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package lib.search.engine;

import com.google.common.collect.TreeMultimap;
import java.util.TreeMap;
import lib.scripture.Chapter;
import lib.scripture.Verse;
import lib.sort.Sort;
import lib.sort.Sortable;

/**
 * Поиск текста в одной главе.
 * @author Толстых Владислав
 */
public class Haystack {
    private final Chapter NEXT_CHAPT;
    private TreeMultimap<Chapter,Verse> response;
    
    /**
     * Новый "стог сена".
     * @param nextChapt следующая глава
     */
    public Haystack(Chapter nextChapt) {
        NEXT_CHAPT=nextChapt;
        response = TreeMultimap.create(new Sortable(Sort.CHAPTER), new Sortable(Sort.VERSE));
    }
    
    /**
     * Получаем результаты поиска.
     * @return результат для этой главы
     */
    public TreeMultimap<Chapter,Verse> found(){
        return response;
    }
    
    /**
     * Отделяем семена от плевел.
     * @param needle иголка
     */
    public void lookingForAneedle(Needle needle) {            
        //получаем стих главы
        while (NEXT_CHAPT.hasMoreElements()) {
            Verse nextVerse = NEXT_CHAPT.nextElement();
            //берём тримап для этого стиха
            TreeMap<Integer, Character> tm = nextVerse.hash();
            //будем искать совпадения
            Scanner scan = new Scanner(NEXT_CHAPT, nextVerse);
            scan.left2Right(tm, needle);       
            if(scan.result()!=null){
                response.putAll(scan.result());
            }
        }
    }
}
